1 #import "BBObjectCell.h"
3 @implementation BBObjectCell
5 - (NSCellImagePosition)preferredImagePosition {
9 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
10 BOOL isFirstResponder = [[controlView window] firstResponder] == controlView && ![controlView isKindOfClass:[NSTableView class]];
11 BOOL dropTarget = ([self isHighlighted] && ([self highlightsBy] & NSChangeBackgroundCellMask) && ![self isBezeled]);
14 NSColor *strokeColor = [NSColor clearColor];
16 if (isFirstResponder) {
17 fillColor = [NSColor selectedTextBackgroundColor];
19 fillColor = [NSColor clearColor];
23 fillColor = [NSColor controlAccentColor];
27 [strokeColor setStroke];
29 NSBezierPath *roundRect = [NSBezierPath bezierPath];
30 [roundRect appendBezierPathWithRoundedRectangle:cellFrame withRadius:cellRadiusFactor];
33 [self drawInteriorWithFrame:[self drawingRectForBounds:cellFrame] inView:controlView];
36 - (NSRect)titleRectForBounds:(NSRect)theRect
38 NSRect rect = theRect;
39 rect = NSOffsetRect(rect, 0, -4);
40 return [super titleRectForBounds: rect];
43 - (void)drawTextForObject:(QSObject *)drawObject withFrame:(NSRect)cellFrame inView:(NSView *)controlView {
44 if ([self imagePosition] == NSImageOnly) return;
46 NSString *abbrString = nil;
47 if ([controlView respondsToSelector:@selector(matchedString)])
48 abbrString = [(QSSearchObjectView *)controlView matchedString];
50 NSString *nameString = nil;
51 NSIndexSet *hitMask = nil;
53 id ranker = [drawObject ranker];
54 if (ranker && abbrString)
55 nameString = [ranker matchedStringForAbbreviation:abbrString hitmask:&hitMask inContext:nil];
58 nameString = [drawObject displayName];
60 BOOL rankedStringIsName = [nameString isEqualToString:[drawObject displayName]] || nameString == nil;
62 // fall back to the identifier if no reasonable name can be found
63 nameString = [drawObject identifier];
66 // Couldn't find anything sensible to use for the name, fallback to avoid a crash
67 nameString = @"Unknown";
70 BOOL useAlternateColor = [controlView isKindOfClass:[NSTableView class]] && [(NSTableView *)controlView isRowSelected:[(NSTableView *)controlView rowAtPoint:cellFrame.origin]];
71 NSColor *mainColor = (textColor ? textColor : (useAlternateColor ? [NSColor alternateSelectedControlTextColor] : [NSColor controlTextColor]));
72 NSColor *fadedColor = [mainColor colorWithAlphaComponent:0.50];
73 NSRect textDrawRect = [self titleRectForBounds:cellFrame];
75 NSMutableAttributedString *titleString = [[[NSMutableAttributedString alloc] initWithString:nameString] autorelease];
76 [titleString setAttributes:rankedStringIsName ? nameAttributes : detailsAttributes range:NSMakeRange(0, [titleString length])];
78 // Bring out the matched letters
79 if (abbrString && ![abbrString hasPrefix:@"QSActionMnemonic"]) {
80 [titleString addAttribute:NSForegroundColorAttributeName value:rankedStringIsName ? fadedColor : [fadedColor colorWithAlphaComponent:0.8] range:NSMakeRange(0, [titleString length])];
84 NSUInteger hits[[titleString length]];
85 NSUInteger count = [hitMask getIndexes:(NSUInteger *)&hits maxCount:[titleString length] inIndexRange:nil];
86 NSDictionary *attributes = @{
87 NSForegroundColorAttributeName: rankedStringIsName ? mainColor : fadedColor
89 for(i = 0; i<count; i += j) {
90 for (j = 1; i+j<count && hits[i+j-1] +1 == hits[i+j]; j++);
91 [titleString addAttributes:attributes range:NSMakeRange(hits[i], j)];
94 [titleString addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithDouble:-1.0] range:NSMakeRange(0, [titleString length])];
97 // // Ranked string and nameString aren't the same. Show 'nameString ⟷ rankedString' in the UI
98 // if (!rankedStringIsName && [drawObject displayName].length) {
99 // [titleString addAttribute:NSFontAttributeName value:detailsFont range:NSMakeRange(0,[titleString length])];
100 // NSMutableAttributedString *attributedNameString = [[NSMutableAttributedString alloc] initWithString:[drawObject displayName]];
101 // [attributedNameString setAttributes:nameAttributes range:NSMakeRange(0, [[drawObject displayName] length])];
103 // [attributedNameString appendAttributedString:[[[NSAttributedString alloc] initWithString:@" ⟷ " attributes:rankedNameAttributes] autorelease]];
104 // // the replaceCharacters... method inserts the new string into the receiver at the start of the work (range.location and range.length are 0)
105 // [titleString replaceCharactersInRange:NSMakeRange(0,0) withAttributedString:attributedNameString];
106 // [attributedNameString release];
110 NSString *detailsString = [drawObject details];
112 NSRange returnRange = [detailsString rangeOfString:@"\n"];
113 if (returnRange.location != NSNotFound) {
114 detailsString = [detailsString substringToIndex:returnRange.location];
117 detailsAttributes = [detailsAttributes mutableCopy];
118 [detailsAttributes setValue:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
120 if (detailsString && detailsString.length && ![detailsString isEqualToString:nameString]) {
121 [titleString appendAttributedString:[[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%@",detailsString] attributes:detailsAttributes] autorelease]];
125 NSRect centerRect = rectFromSize([titleString size]);
126 centerRect.size.width = NSWidth(textDrawRect);
127 centerRect.size.height = MIN(NSHeight(textDrawRect), centerRect.size.height);
128 [titleString drawInRect:centerRectInRect(centerRect, textDrawRect)];
131 - (void)drawSearchPlaceholderWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
132 NSString *defaultText = NSLocalizedStringWithDefaultValue(@"Type to search", nil, [NSBundle mainBundle], @"Type to search", @"Hint that appears in the first pane of the QS interface when it's empty.");
133 NSSize textSize = [defaultText sizeWithAttributes:nameAttributes];
134 NSRect textRect = centerRectInRect(rectFromSize(textSize), cellFrame);
135 BOOL isFirstResponder = [[controlView window] firstResponder] == controlView && ![controlView isKindOfClass:[NSTableView class]];
137 if (isFirstResponder && [controlView isKindOfClass:[QSSearchObjectView class]]) {
138 NSImage *find = [NSImage imageWithSystemSymbolName:@"magnifyingglass.circle.fill" accessibilityDescription:nil];
141 [find setSize:QSSize16];
142 NSRect findImageRect = expelRectFromRectOnEdge(centerRectInRect(rectFromSize([find size]), cellFrame), textRect, NSRectEdgeMinX, -2);
145 NSGraphicsContext *graphicsContext = [NSGraphicsContext currentContext];
146 [graphicsContext saveGraphicsState];
147 CGContextRef context = [graphicsContext CGContext];
148 CGContextBeginTransparencyLayerWithRect(context, findImageRect, nil);
149 CGContextSetBlendMode(context, kCGBlendModeNormal);
150 [find drawInRect:findImageRect fromRect:rectFromSize([find size]) operation:NSCompositingOperationSourceOver fraction:1];
151 CGContextSetBlendMode(context, kCGBlendModeSourceIn);
152 CGContextSetFillColorWithColor(context, [[NSColor textColor] CGColor]);
153 CGContextFillRect(context, findImageRect);
154 CGContextEndTransparencyLayer(context);
156 [defaultText drawInRect:textRect withAttributes:nameAttributes];